package com.demo.villageresidence.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.villageresidence.dto.Result;
import com.demo.villageresidence.pojo.Activity;
import com.demo.villageresidence.pojo.Admin;
import com.demo.villageresidence.pojo.Bidding;
import com.demo.villageresidence.service.ActivityService;
import com.demo.villageresidence.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/activity")
public class ActivityController {

    @Resource
    private ActivityService activityService;

    @Autowired
    private AdminService adminService;

    // 添加活动


    // 分页查询日常活动
    @GetMapping("getEverydayActivityByPage")
    public Result getEverydayActivityByPage(@RequestParam(value = "current", defaultValue = "1") Integer current,
                                  @RequestParam(value = "pageSize", defaultValue = "10")  Integer pageSize) {
        // 根据用户查询 日常活动
        Page<Activity> page = activityService.query().eq("activity_type", 1)
                .orderByDesc("activity_releaseTime")
                .page(new Page<>(current, pageSize));
        // 获取当前页数据
        List<Activity> records = page.getRecords();
        // 获取总条数
        long total = page.getTotal();
        return Result.ok(records,total);
    }


    // 分页查询志愿者活动
    @GetMapping("getVolunteerActivityByPage")
    public Result getVolunteerActivityByPage(@RequestParam(value = "current", defaultValue = "1") Integer current,
                                    @RequestParam(value = "pageSize", defaultValue = "10")  Integer pageSize) {
        // 根据用户查询 日常活动
        Page<Activity> page = activityService.query().eq("activity_type", 0)
                .orderByDesc("activity_releaseTime")
                .page(new Page<>(current, pageSize));
        // 获取当前页数据
        List<Activity> records = page.getRecords();
        records.forEach(activity -> {
            Admin admin = adminService.getById(activity.getAdminId());
            activity.setAdmin(admin);
        });
        // 获取总条数
        long total = page.getTotal();
        return Result.ok(records,total);
    }

    @GetMapping("getAllVolunteerActivity")
    public Result getAllVolunteerActivity() {
        // 根据用户查询 日常活动
        List<Activity> activities = activityService.query()
                .eq("activity_type", 0)
                .orderByDesc("activity_releaseTime")
                .list();
        activities.forEach(activity -> {
            Admin admin = adminService.getById(activity.getAdminId());
            activity.setAdmin(admin);
        });
        return Result.ok(activities);
    }


    @GetMapping("getAllEverydayActivity")
    public Result getAllEverydayActivity() {
        // 根据用户查询 日常活动
        List<Activity> activities = activityService.query()
                .eq("activity_type", 1)
                .orderByDesc("activity_releaseTime")
                .list();
        activities.forEach(activity -> {
            Admin admin = adminService.getById(activity.getAdminId());
            activity.setAdmin(admin);
        });
        return Result.ok(activities);
    }

    // 根据id查询活动
    @GetMapping("getActivityById/{id}")
    public Result getActivityById(@PathVariable("id") Integer id) {
        Activity activity = activityService.getById(id);
        return Result.ok(activity);
    }

    // 根据id修改活动
    @PutMapping("updateActivityById")
    public Result updateNotice(@RequestBody Activity activity) {
        if (activity.getActivityDigest().length() >= 200){
            activity.setActivityDigest(activity.getActivityDigest().substring(0, 200));
            System.out.println("活动内容过长，已经截取为200个字符");
        }else {
            activity.setActivityDigest(activity.getActivityDigest());
        }

        if (activityService.updateById(activity) != true){
            return Result.fail("修改失败");
        }
        return Result.ok("修改成功");
    }

    @PostMapping("addActivity")
    public Result addActivity(@RequestBody Activity activity) {
        if(activity.getActivityStarttime().before(activity.getActivityDeadline()))
            return Result.fail("活动开始时间不能在报名截止时间之前，请检查！");
        if (activity.getActivityDigest().length() >= 200){
            activity.setActivityDigest(activity.getActivityDigest().substring(0, 200));
            System.out.println("活动内容过长，已经截取为200个字符");
        }else {
            activity.setActivityDigest(activity.getActivityDigest());
        }
        activity.setIsDelete(0);
        // 获取当前日期
        LocalDateTime localDate = LocalDateTime.now();
        activity.setActivityReleasetime(localDate);
        boolean save = activityService.save(activity);
        if (save == false)
            return Result.fail("添加失败");
        return Result.ok("添加成功");
    }



}
